<?php

class NoticeController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;


	public function init() {

		$this->commonAction();	

	}
	//用户首页
	public function indexAction() {
		try {
		$count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
			    $this->view->pageNum=$curpage;		
    			$offset = ($curpage - 1) * $count; 
			 $sql="select SQL_CALC_FOUND_ROWS *  from  notice_central    limit $offset,$count";
		    
			 $result = $this->_dbAdapter->fetchAll ($sql);  
			  
		    	$this->view->result= $result;
			 
    			$param ['file'] = '/' . $this->controller . '/' . $this->action;
    			$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    			$param ['perpagenum'] = $count; // 每页显示的数目
    			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    			$param ['curpage'] = $curpage; // 当前页码
				$this->view->allnum= $param ['totalnum'];

    			//	require_once 'MyClass/class.page.php';
    			$page = new MyClass_Page ( $param );
    		//	$page->setvar ( array ('year' => $year,'week'=>$week,'foodtimeid'=>$foodtimeid) );
				$this->view->page= $page->getNumPage ();
		
		
			 
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	} 
		
		 
	public function opAction() {
		try {
			if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
			}
			
			$p =$this->_request->getPost ();
			//print_r($p); exit;
			if(empty($p['notice_title'])){
				throw new Exception ( '标题不能为空！' );
				} 
			if(empty($p['notice_type'])){
				throw new Exception ( '请选择公告类型' );
				} 
			if(empty($p['notice_content'])){
				throw new Exception ( '内容不能为空！' );
				}
			 
		    $data=array();
			 foreach($p as $key => $value){
				  $data[$key] = $value;	 
					} 
		  $data['adduserid']=$this->_user->id;
		  $data['addtime']=time();	 
		  $result=$this->_dbAdapter ->insert ( 'notice_central',$data );
		  
		  $sql='select groupid from groups where 1';
		  $groupid=$this->_dbAdapter->fetchCol($sql);
		  //获取post提交的值 			    
		  $time=time();
		  //获取生成的id号 
		  $noticekey=$this->fast_uuid();

		  foreach($groupid as $key=>$value){
			  $data=array(
			  	  'schoolid'=>0,
				  'classid'=>0,
			  );
		  }
		  
		  if($result){
				$this->feedback ( '发布成功', '成功', '/'.$this->controller.'/list', 'tip' );
		    }else{	
				throw new Exception ( '更新失败 ！' );
			} 
			
			//print_r($p);exit;
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	//查看公告列表
	public function addAction() {
		try {
	        
	 
	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	//修改公告 
	public function modifiyAction() {
		try {
	    $id = ( int ) $this->_request->getParam ( 'id' );    
	    $pageNum = ( int ) $this->_request->getParam ( 'pageNum' ); 
	    if(empty($id)){ 
		   throw new Exception ( '参数错误！' );
		   }
	     //查询
		  $sql="select *  from  notice_central  where noticeid='".$id."'";
		  $result = $this->_dbAdapter->fetchRow ($sql);  
		  $this->view->result= $result;	 
		  
    	} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	
	
	
	public function opmodifiyAction() {
		try {
			 if (! $this->isPost ()) {
					throw new Exception ( '参数错误！' );
				} 
			 $p =$this->_request->getPost ();
		   
		   	$data=array();
			 foreach($p as $key => $value){
				  $data[$key] = $value;	 
			   }	 
	     $result=$this->_dbAdapter ->update ( 'notice_central',$data,'noticeid='.$p['noticeid'] );
		  
		  if($result){
				$this->feedback ( '修改成功', '成功', '/'.$this->controller.'/list', 'tip' );
		      }else{	
				throw new Exception ( '操作失败 ！' );
			 } 
	 
	     } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function deleteAction() {
		try {
		 $id = ( int ) $this->_request->getParam ( 'id' );    
	     $pageNum = ( int ) $this->_request->getParam ( 'pageNum' ); 
		 
	      if(empty($id)){ 
		      throw new Exception ( '参数错误！' );
		    }
		    	  
	      $result=$this->_dbAdapter ->delete ( 'notice_central','noticeid='.$id );
		  
		  if($result){
				$this->feedback ( '删除成功', '成功', '/'.$this->controller.'/list/page/'.$pageNum, 'tip' );
		      }else{	
				throw new Exception ( '操作失败 ！' );
			 } 
	 
	     } catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	 private function fast_uuid($suffix_len=3){
        //! 计算种子数的开始时间
        static $being_timestamp = 1216681180;// 2012-5-10
       
        $time = explode(' ', microtime());
        $id = ($time[1] - $being_timestamp) . sprintf('%06u', substr($time[0], 2, 6));
        if ($suffix_len > 0)
        {
            $id .= substr(sprintf('%010u', mt_rand()), 0, $suffix_len);
        }
        return $id;
    }
	
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}}

